﻿using System;
using System.Drawing;
using Microsoft.Win32;

namespace PicGame.Utils
{
    internal sealed class Reg
    {
        private string _key = @"Software\MySoftWare\PicGame";

        private Reg()
        {

        }

        private static Reg _instance;

        public static Reg Instance
        {
            get
            {
                if (Reg._instance == null)
                {
                    Reg._instance = new Reg();
                }

                return Reg._instance;
            }
        }

        /// <summary>
        /// 把窗体的大小位置等信息写入注册表
        /// </summary>
        /// <param name="point"></param>
        /// <param name="size"></param>
        public void RegForm(Point point, Size size)
        {
            try
            {
                using (RegistryKey regkMain = Registry.CurrentUser)
                {
                    using (RegistryKey regk = regkMain.CreateSubKey(this._key))
                    {
                        regk.SetValue("Left", point.X);
                        regk.SetValue("Top", point.Y);
                        regk.SetValue("Width", size.Width);
                        regk.SetValue("Height", size.Height);
                    }
                }
            }
            catch
            {

            }
        }

        /// <summary>
        /// 程序按运行时按照记录的信息恢复窗体上次状态。
        /// </summary>
        /// <param name="point"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public bool LoadForm(out Point point, out Size size)
        {
            try
            {
                using (RegistryKey regkMain = Registry.CurrentUser)
                {
                    using (RegistryKey regk = regkMain.OpenSubKey(this._key))
                    {
                        if (regk != null)
                        {
                            var left = regk.GetValue("Left");
                            var top = regk.GetValue("Top");
                            var width = regk.GetValue("Width");
                            var height = regk.GetValue("Height");

                            if (left != null && top != null && width != null && height != null)
                            {
                                point = new Point(Convert.ToInt16(left), Convert.ToInt16(top));

                                size = new Size(Convert.ToInt16(width), Convert.ToInt16(height));

                                return true;
                            }
                        }
                    }
                }
            }
            catch
            {

            }

            point = Point.Empty;
            size = Size.Empty;
            return false;
        }
    }
}